package com.example.leaveplushjava.controller;

import com.example.leaveplushjava.ThrowData.Result;
import com.example.leaveplushjava.domain.LeaveDomain;
import com.example.leaveplushjava.service.LeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @RestController
 * Spring 中 @RestController的作用等同于@Controller + @ResponseBody。
 * @RestController 该类中所有的API接口返回的数据 它会以Json字符串的形式返回给客户端
 * @Controller 可以返回jsp 要想返回json使用@ResponseBody 如果使用@RestController就可以简化
 * @Controller 将当前修饰的类注入SpringBoot IOC容器 让SpringBoot来管控
 */

/**
 * @RequestMapping
 * @RequestMapping(path = "/login", method={RequestMethod.POST,RequestMethod.GET})
 * 参数 value 和 path 可以互换 如果只有个路径时 可以直接省略
 */
@RestController
@RequestMapping(value = "/admin")
@Slf4j
public class LeaveController {
    @Resource
    private LeaveService leaveService;

    @RequestMapping(value = "/add")
    public Result add(@RequestBody LeaveDomain leaveDomain){
        Result result = new Result();
        try {
            return leaveService.addLeave(leaveDomain);
        }catch (Exception e){
            log.error("插入错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping(value = "/search")
    public Result search(@RequestBody LeaveDomain leaveDomain){
        Result result = new Result();
        try {
            return leaveService.listLeave(leaveDomain);
        }catch (Exception e){
            log.error("查询错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }
}
